package com.polaris.live.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.adapter.SearchUserViewHolder.Companion.TYPE_ROOM
import com.polaris.live.adapter.SearchUserViewHolder.Companion.TYPE_USER
import com.polaris.live.adapter.home.FriendShipAdapter
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.databinding.AdapterFriendShipBinding
import com.polaris.live.databinding.AdapterSearchUserBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.CommonUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * SearchUserAdapter
 *
 * @author Created by 半仙 on 2024/9/10/010 16:02
 */
class SearchUserAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    init {
        addChildClickViewIds(R.id.room_cl, R.id.user_cl, R.id.av_head)
    }

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> TYPE_ROOM
            is UserBasicVo -> TYPE_USER
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_USER -> SearchUserViewHolder(
                AdapterSearchUserBinding.inflate(layoutInflater, parent, false)
            )

            TYPE_ROOM -> FriendShipAdapter.FriendShipViewHolder(
                AdapterFriendShipBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is FriendShipAdapter.FriendShipViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is SearchUserViewHolder -> if (item is UserBasicVo) holder.bind(item)
        }
    }
}

class SearchUserViewHolder(binding: AdapterSearchUserBinding) :
    BaseBindingViewHolder<AdapterSearchUserBinding, UserBasicVo>(binding) {

    override fun bind(item: UserBasicVo) {
        mBinding.apply {
            avHead.setImageUrl(item.avatar, item.userId)
            tvNickName.text = item.nickname
            widgetCountry.setCountry(item.country)
            vipView.setVipLevel(item.vipLevel)
            if (CommonUtils.isMan(item.sex)) {
                widgetLevel.setRichsLevel(item.richs)
            } else {
                widgetLevel.setCharmsLevel(item.charms)
            }
            widgetId.setId(item)
        }
    }

    companion object {

        const val TYPE_ROOM = 0
        const val TYPE_USER = 1
    }

}